package ace.module.account.test.core.listener;

import ace.cmp.core.constants.CoreConstant;
import ace.module.account.api.enums.IamAccountPassportPassportTypeEnum;
import ace.module.account.core.impl.constant.AccountCoreConstant;
import ace.module.account.core.impl.dao.entity.IamAccount;
import ace.module.account.core.impl.dao.entity.IamAccountPassport;
import ace.module.account.core.impl.dao.entity.IamApplication;
import ace.module.account.core.impl.dao.repository.IamAccountPassportRepository;
import ace.module.account.core.impl.dao.repository.IamAccountRepository;
import ace.module.account.core.impl.dao.repository.IamApplicationRepository;
import ace.module.common.api.constant.CommonConstants;
import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/3/22 14:42
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class IamAccountInitTestListener implements ApplicationListener<ApplicationStartedEvent> {

  private final IamApplicationRepository iamApplicationRepository;
  private final IamAccountRepository iamAccountRepository;
  private final IamAccountPassportRepository iamAccountPassportRepository;

  @Override
  public void onApplicationEvent(ApplicationStartedEvent event) {
    this.init();
  }

  private void init() {
    IamApplication iamApplication =
        iamApplicationRepository.getById(CommonConstants.DEFAULT_APP_ID);
    IamAccount iamAccount = iamAccountRepository.getById(AccountCoreConstant.DEFAULT_ACCOUNT_ID);
    IamAccountPassport iamAccountPhonePassport =
        IamAccountPassport.builder()
            .id(AccountCoreConstant.DEFAULT_PHONE_PASSPORT_ID)
            .iamAppId(iamApplication.getId())
            .iamAccountId(iamAccount.getId())
            .passport(AccountCoreConstant.DEFAULT_ADMIN_ACCOUNT_PHONE_PASSPORT)
            .passport2("")
            .passport3("")
            .passportType(IamAccountPassportPassportTypeEnum.PHONE.getCode())
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    IamAccountPassport iamAccountWxMpOpenIdPassport =
        IamAccountPassport.builder()
            .id(AccountCoreConstant.DEFAULT_WX_MP_OPENID_PASSPORT_ID)
            .iamAppId(iamApplication.getId())
            .iamAccountId(iamAccount.getId())
            .passport(CommonConstants.DEFAULT_WEI_XIN_MINI_APP_ID)
            .passport2(AccountCoreConstant.DEFAULT_WX_MP_OPENID_PASSPORT)
            .passport3("")
            .passportType(IamAccountPassportPassportTypeEnum.WX_MINI_APP_OPENID.getCode())
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    IamAccountPassport iamAccountWxMpUnionIdPassport =
        IamAccountPassport.builder()
            .id(AccountCoreConstant.DEFAULT_WX_MP_UNIONID_PASSPORT_ID)
            .iamAppId(iamApplication.getId())
            .iamAccountId(iamAccount.getId())
            .passport(CommonConstants.DEFAULT_WEI_XIN_MINI_APP_ID)
            .passport2(AccountCoreConstant.DEFAULT_WX_MP_UNIONID_PASSPORT)
            .passport3("")
            .passportType(IamAccountPassportPassportTypeEnum.WX_MINI_APP_UNIONID.getCode())
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    this.iamAccountPassportRepository.insert(iamAccountPhonePassport);
    this.iamAccountPassportRepository.insert(iamAccountWxMpUnionIdPassport);
    this.iamAccountPassportRepository.insert(iamAccountWxMpOpenIdPassport);
  }
}
